package com.wmlive.hhvideo.heihei.mainhome.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;

import com.wmlive.hhvideo.common.GlobalParams;
import com.wmlive.hhvideo.common.base.DcBaseActivity;
import com.wmlive.hhvideo.heihei.beans.main.MultiTypeVideoBean;
import com.wmlive.hhvideo.heihei.mainhome.adapter.MainNewViewPagerAdapter;
import com.wmlive.hhvideo.heihei.mainhome.fragment.InfoFragment;
import com.wmlive.hhvideo.heihei.mainhome.fragment.RecommendFragmentNew;
import com.wmlive.hhvideo.heihei.mainhome.presenter.UserInfoCallback;
import com.wmlive.hhvideo.widget.UnScrollViewPager;
import com.wmlive.hhvideo.widget.dialog.MyDialog;
import com.wmlive.hhvideo.widget.dialog.MyDialogClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.wmlive.hhvideo.R;
import cn.wmlive.hhvideo.rxbuslib.RxBus2;

/**
 * 视频播放列表页面
 * 话题、音乐、用户，最新、单个列表
 */
public class VideoListActivity extends DcBaseActivity implements ViewPager.OnPageChangeListener, UserInfoCallback {

    @BindView(R.id.vpContainer)
    public UnScrollViewPager vpContainer;

    private List<Fragment> fragmentList;
    private MainNewViewPagerAdapter pagerAdapter;
    public int currentPage = 0;//当前页
    private RecommendFragmentNew recommendFragmentNew;
    private InfoFragment infoFragment;
    private MyDialog useMobileDialog;//使用移动网络的Dialog

    /**
     * @param context
     * @param videoType           视频的类型，参考{@link RecommendFragmentNew }中{@link RecommendFragmentNew#videoType}的Type参数
     * @param userShortVideoParam 根据{@link RecommendFragmentNew#videoType}所传的参数{@link MultiTypeVideoBean}
     */
    public static void startActivity(Context context, int videoType, MultiTypeVideoBean userShortVideoParam) {
        Intent intent = new Intent(context, VideoListActivity.class);
        intent.putExtra("videoType", videoType);
        intent.putExtra("beans", userShortVideoParam);
        context.startActivity(intent);
    }

    @Override
    protected void onSingleClick(View v) {

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_video_list;
    }

    @Override
    protected void initData() {
        super.initData();
        setRelativeMode(true);
        setTitle("", true);
//        toolbar.setBackgroundResource(R.drawable.bg_main_title);
        changeDecorView(3);
        Intent intent = getIntent();
        if (intent != null) {
            RxBus2.get().send(GlobalParams.EventType.TYPE_SHOW_VIDEO_COVER, true);
            vpContainer.setScrollable(true);
            fragmentList = new ArrayList<>();
            recommendFragmentNew = RecommendFragmentNew.newInstance(intent.getIntExtra("videoType", RecommendFragmentNew.TYPE_USER),
                    (MultiTypeVideoBean) intent.getSerializableExtra("beans"));
            infoFragment = InfoFragment.newInstance(false);
            fragmentList.add(recommendFragmentNew);
            fragmentList.add(infoFragment);
            recommendFragmentNew.setUserInfoCallback(this);
            pagerAdapter = new MainNewViewPagerAdapter(getSupportFragmentManager(), fragmentList);
            vpContainer.setAdapter(pagerAdapter);
            vpContainer.addOnPageChangeListener(this);

        } else {
            toastFinish();
        }
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        currentPage = position;
        if (null != onPageChangeListener) {
            onPageChangeListener.onPageChanged(currentPage);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    @Override
    public void onBack() {
        onKeyDown(KeyEvent.KEYCODE_BACK, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (currentPage == 1) {
                currentPage = 0;
                if (null != onPageChangeListener) {
                    onPageChangeListener.onPageChanged(currentPage);
                }
                vpContainer.setCurrentItem(0, true);
                return true;
            }
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void getUserInfo(long userId) {
        if (userId > 0) {
            if (infoFragment != null) {
                infoFragment.getUserInfo(userId);
            }
        }
    }


    private OnPageChangeListener onPageChangeListener;

    public void setOnPageChangeListener(OnPageChangeListener onPageChangeListener) {
        this.onPageChangeListener = onPageChangeListener;
    }

    public interface OnPageChangeListener {
        void onPageChanged(int currentPage);
    }

    //显示是否使用移动网络播放视频
    public void showUseMobileNetwork() {
        if (useMobileDialog == null) {
            useMobileDialog = new MyDialog("", getString(R.string.stringMobileNetworkHint), "是", "否", false, new MyDialogClickListener() {
                @Override
                public void onPositiveClick(DialogInterface dialog, int which) {
                    super.onPositiveClick(dialog, which);
                    GlobalParams.StaticVariable.sAllowdMobile = true;
                }

                @Override
                public void onNegativeClick(DialogInterface dialog, int which) {
                    super.onNegativeClick(dialog, which);
                    GlobalParams.StaticVariable.sAllowdMobile = false;
                }
            });
        }
        if (!useMobileDialog.isShowing()) {
            useMobileDialog.show(this);
        }
    }

    //恢复播放
    public void resumePlay() {
        if (null != recommendFragmentNew) {
            recommendFragmentNew.resumePlay();
        }
    }
}
